package com.suxin.ble.demo;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.widget.Toast;

import com.clj.fastble.BleManager;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.utils.HexUtil;

import java.util.List;
import java.util.Set;

public class Utils {
    public static final String deviceName = "SuXin Iot";

    public static BleDevice getDevice(Context context) {
        List<BleDevice> allConnectedDevice = BleManager.getInstance().getAllConnectedDevice();
        if (allConnectedDevice.size() == 0) {
            return null;
        }
        for (int i = 0; i < allConnectedDevice.size(); i++) {
            if (allConnectedDevice.get(i).getName().equals(deviceName)) {
                return allConnectedDevice.get(i);
            }
        }
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static BluetoothGattService getService(Context context, BleDevice bleDevice, int serviceIndex) {
        List<BleDevice> allConnectedDevice = BleManager.getInstance().getAllConnectedDevice();
        if (allConnectedDevice.size() == 0) {
            showToast(context, "暂无设备连接");
            return null;
        }
        BluetoothGatt bluetoothGatt = BleManager.getInstance().getBluetoothGatt(bleDevice);
        BluetoothGattService bluetoothGattService = bluetoothGatt.getServices().get(serviceIndex);
        return bluetoothGattService;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static BluetoothGattCharacteristic getCharasterstic(BluetoothGattService service, int charasterticIndex) {
        if (service == null) {
            return null;
        }
        BluetoothGattCharacteristic bluetoothGattCharacteristic = service.getCharacteristics().get(charasterticIndex);
        return bluetoothGattCharacteristic;
    }

    public static void showToast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    @NonNull
    public static String switchResult(byte[] data) {
        String result = HexUtil.formatHexString(data, false);
        String result1 = "";
        String result2 = "";
        String result3 = "";
        String result4 = "";
        String temp = "";

        result2 = String.valueOf(result.charAt(3));
        result1 = String.valueOf(result.charAt(2));
        result4 = String.valueOf(result.charAt(1));
        result3 = String.valueOf(result.charAt(0));

        temp = result1 + result2 + result3 + result4;
        return temp;
    }
    /*
     * 加速度转换工具
     * */

    public static float switchAcc(int acc) {
        return (float) (((acc - 1024) * 9.8) / 1024);
    }

    public static float getAccResult(int acc) {
        return (float) (((1024 - acc) * 9.8) / 1024);
    }


    public static float getAccResult1(int acc) {
        return (float) (((acc - 1024) * 9.8) / 1024);
    }

}
